<template>
	<view class="box box-card">
		<uni-forms ref="updatePwdForm" :modelValue="updatePwdForm" labelWidth="70px" errShowType="toast">
			<uni-forms-item name="oldPwd" label="旧密码">
				<uni-easyinput type="password" v-model="updatePwdForm.oldPwd" placeholder="旧密码" maxlength="20">
				</uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="newPwd" label="新密码">
				<uni-easyinput type="password" v-model="updatePwdForm.newPwd" placeholder="新密码" maxlength="20">
				</uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="comfirmPwd" label="确认密码">
				<uni-easyinput type="password" v-model="updatePwdForm.comfirmPwd" placeholder="确认密码" maxlength="20">
				</uni-easyinput>
			</uni-forms-item>
			<uni-forms-item labelWidth="0px">
				<button class="form-button uni-mt-10" @click="update">保存</button>
			</uni-forms-item>
		</uni-forms>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				updatePwdForm: {
					oldPwd: '',
					newPwd: '',
					comfirmPwd: '',
					checkOldPwd: 'Y'
				},
				formRules: {
					newPwd: {
						rules: [{
							required: true,
							errorMessage: '请输入新密码'
						}]
					},
					comfirmPwd: {
						rules: [{
							required: true,
							errorMessage: '请输入确认密码'
						}, {
							validateFunction: function(rule, value, data, callback) {
								if (value === data.newPwd) {
									return true;
								} else {
									callback('新密码输入不一致，请重新输入')
								}
							}
						}]
					}
				}
			}
		},
		onReady() {
			this.$refs.updatePwdForm.setRules(this.formRules)
		},
		methods: {
			update() {
				this.$refs.updatePwdForm.validate().then(() => {
					this.$api.post('/appUser/updatePwd', this.updatePwdForm).then(res => {

						uni.navigateBack({
							delta: 1,
							success: () => {
								this.$util.messageTip(res.message);
							}
						})
					}).catch(err => {
						this.$util.messageTip(err.message);
					})
				})
			}
		}
	}
</script>
